package com.sh.instructor.service.impl;

import com.sh.instructor.dao.StaffDao;
import com.sh.instructor.service.StaffService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import sh.dto.Response;
import sh.dto.ResultDto;
import sh.entity.Staff;
import sh.exception.SystemException;
import sh.util.MD5Util;

import javax.annotation.Resource;
import java.util.List;

/**
 * 教职员工表(Staff)表服务实现类
 *
 * @author makejava
 * @since 2021-05-10 16:28:16
 */
@Service("staffService")

public class StaffServiceImpl implements StaffService {
    @Resource
    private StaffDao staffDao;

    /**
     * 通过ID查询单条数据
     *
     * @param staffId 主键
     * @return 实例对象
     */
    @Override
    public ResultDto queryById(Integer staffId) {
        return Response.success(200,"success",this.staffDao.queryById(staffId));
    }

    /**
     * 修改数据
     *
     * @param staff 实例对象
     * @return 实例对象
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public ResultDto update(Staff staff) {
        if (staff==null) {
            throw new SystemException(1001,"数据有误！");
        }
        //密码加密
        //如果修改了密码进行加密
        if (staff.getStaffPassword()!=null) {
            staff.setStaffPassword(MD5Util.MD55(staff.getStaffPassword()));
        }
        int update = this.staffDao.update(staff);
        if (update>0) {
            return Response.success(200,"修改数据成功！");
        }
        throw new SystemException(1001,"修改失败！");
    }
}
